package com.xsherl.leetcode.solution;

/**
 */
public class MaxScore {

    public static int maxScore(int[] cardPoints, int k) {
        int length = cardPoints.length - 1;
        int maxSum = 0, preSum = 0;
        for (int i = 0; i < k; ++i) {
            maxSum += cardPoints[i];
        }
        preSum = maxSum;
        for (int j = 0; j < k; j++) {
            preSum += cardPoints[length - j] - cardPoints[k - j - 1];
            if (preSum > maxSum) {
                maxSum = preSum;
            }
        }
        return maxSum;
    }

}
